/*
 * @Author: 灰灰
 * @Date: 2022-01-10 15:19:31
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-01-11 10:42:59
 * @Description: 
 */

export default {
  data() {
    return {
      loading: false,
      startPage: 1, // 开始页面
      pageData: {
        pageNum: 1,
        pageSize: 15,
      },
      pageSizes: [15, 30, 50, 100],
      pageLayout: 'total, sizes, prev, pager, next',
      tTotal: 0, // 总数
      queryParams: {},
      otherParams: {},
      dataList: [],
      pageApi: null,
      initLoad: true, // 首次进入是否加载数据
      autoHander: true, // 自动处理数据到dataList
    }
  },
  mounted() {
    this.initLoad && (this.refresh())
  },
  methods: {
    onReset() {
      this.$refs.searchForm && this.$refs.searchForm.resetFields();
      this.refresh();
    },
    customReset() {
      this.queryParams = {};
      this.refresh();
    },
    refresh() {
      this.pageData.pageNum = this.startPage;
      this.loadData(this.pageApi);
    },
    loadData(api) {
      api = api || this.pageApi;
      if (!api) return this.$message.warning('没有页面接口api');

      this.loading = true;
      let query = Object.assign(this.pageData, this.queryParams, this.otherParams);
      api(query).then(res => {
        const data = res.data.items;
        this.tTotal = res.data.total;
        if (this.autoHander) {
          this.dataList = data || [];
        } else {
          this.handerQueryData(data)
        }
      }).finally(() => {
        this.loading = false;
      })
    },
    // 手动处理查询数据
    handerQueryData(data) {

    },
    // 切换页码
    currentChange(val) {
      this.pageData.pageNum = val;
      this.loadData();
    },
    // 切换页数
    sizeChange(val) {
      this.pageData.pageSize = val;
      this.loadData();
    },

    pagingChange(num, size) {
      size != -1 && (this.pageData.pageSize = size);
      num != -1 && (this.pageData.pageNum = num);
      this.loadData();
    }
  }
}